package com.roger.homecenterscreen.voice

import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
class AsrManagerStateTests {
    private fun newCaches(): AsrManager.StateData.Caches {
        return AsrManager.StateData.Caches(
            cumulativeAsrText = StringBuilder(),
            triggerWordDetected = true,
            triggerWordDetectTime = 123L,
            vpActive = true,
            vpExtracted = true,
            recentWriteIndex = 128,
            recentBytesWritten = 2048,
            startRecordingAtMs = 123L,
            recordStartAligned = 456L,
            firstAsrTextAt = 789L,
            lastWsMsgAt = 321L,
            voiceStartAtMs = 654L,
            lastAsrAtMs = 987L
        )
    }

    @Test
    fun forceClear_resetsCachesAndTimestamps() {
        val c = newCaches()
        c.cumulativeAsrText.append("abc")
        val now = 1000L
        val res = AsrManager.StateData.clearOnListeningStart(c, now)
        assertEquals(0, res.cumulativeAsrText.length)
        assertEquals(0, res.recentBytesWritten)
        assertEquals(0, res.recentWriteIndex)
        assertTrue(!res.vpActive)
        assertTrue(!res.vpExtracted)
        assertEquals(now, res.startRecordingAtMs)
    }

    @Test
    fun repeatedClearsAreStable() {
        var c = newCaches()
        for (i in 0 until 1000) {
            c.recentBytesWritten = 2048
            c.recentWriteIndex = 128
            c = AsrManager.StateData.clearOnListeningStart(c, (1000L + i))
            assertEquals(0, c.recentBytesWritten)
            assertEquals(0, c.recentWriteIndex)
        }
    }

    @Test
    fun longRunMemoryStability() {
        var c = newCaches()
        for (i in 0 until 5000) {
            c.recentBytesWritten = 4096
            c.recentWriteIndex = 256
            c = AsrManager.StateData.clearOnListeningStart(c, (1000L + i))
        }
        assertEquals(0, c.recentBytesWritten)
        assertEquals(0, c.recentWriteIndex)
    }

    @Test
    fun recoveryAfterExceptionValues() {
        var c = AsrManager.StateData.Caches(
            cumulativeAsrText = StringBuilder("异常文本"),
            triggerWordDetected = true,
            triggerWordDetectTime = -999L,
            vpActive = true,
            vpExtracted = true,
            recentWriteIndex = Int.MAX_VALUE,
            recentBytesWritten = Int.MAX_VALUE,
            startRecordingAtMs = -1L,
            recordStartAligned = Long.MAX_VALUE,
            firstAsrTextAt = Long.MAX_VALUE,
            lastWsMsgAt = Long.MIN_VALUE,
            voiceStartAtMs = Long.MIN_VALUE,
            lastAsrAtMs = Long.MAX_VALUE
        )
        val now = 2222L
        c = AsrManager.StateData.clearOnListeningStart(c, now)
        assertEquals(0, c.cumulativeAsrText.length)
        assertEquals(0, c.recentBytesWritten)
        assertEquals(0, c.recentWriteIndex)
        assertEquals(now, c.startRecordingAtMs)
        assertEquals(0L, c.recordStartAligned)
        assertEquals(0L, c.firstAsrTextAt)
        assertEquals(0L, c.lastWsMsgAt)
        assertEquals(0L, c.voiceStartAtMs)
        assertEquals(0L, c.lastAsrAtMs)
    }
}
